package com.bw.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.bw.service.TmsService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class TmsController {
    @Autowired
    TmsService tmsService;
    //    统计当日下单总金额（周期：当日，指标：金额）当日：2023-01-06 sql
    @RequestMapping("/total")
    public Object total(@RequestParam("nowDate") String nowDate){
        List<Map> total = tmsService.total(nowDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        jsonObject.put("data",total.get(0).get("total"));
        return jsonObject;
    };
    //    统计各类货物下单金额占比（存储过程） sql
    @RequestMapping("/cargTotal")
    public Object cargTotal(@RequestParam("nowDate") String nowDate){
        List<Map> list = tmsService.cargTotal(nowDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONArray dataArray = new JSONArray();
        for (Map map : list) {
            JSONObject dataObject = new JSONObject();
            dataObject.put("name",map.get("NAME"));
            dataObject.put("value",map.get("zb"));
            dataArray.add(dataObject);
        }
        jsonObject.put("data",dataArray);
        return jsonObject;
    };
    //    统计各机构完成运输次数排名（开窗） sql
    @RequestMapping("/regTotal")
    public Object  regTotal(@RequestParam("nowDate") String nowDate){
        List<Map> list = tmsService.regTotal(nowDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONObject dataObject = new JSONObject();

        JSONArray colArray = new JSONArray();
        JSONObject colObject = new JSONObject();
        colObject.put("name","机构");
        colObject.put("id","subsys");
        colArray.add(colObject);


        JSONObject colObject1 = new JSONObject();
        colObject1.put("name","完运次数");
        colObject1.put("id","module");
        colArray.add(colObject1);
        JSONObject colObject2 = new JSONObject();
        colObject2.put("name","排名");
        colObject2.put("id","business");
        colArray.add(colObject2);

        dataObject.put("columns",colArray);


        JSONArray rowlArray = new JSONArray();
        for (Map map : list) {
            JSONObject rowObject = new JSONObject();
            rowObject.put("subsys",map.get("NAME"));
            rowObject.put("module",map.get("COUNT"));
            rowObject.put("business",map.get("pm"));
            rowlArray.add(rowObject);
        }


        dataObject.put("rows",rowlArray);
        jsonObject.put("data",dataObject);
        return jsonObject;
    };
    //    统计各省份分拣次数 sql
    @RequestMapping("/proCount")
    public Object proCount(@RequestParam("nowDate") String nowDate){
        List<Map> list = tmsService.proCount(nowDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONObject dataObject = new JSONObject();
        JSONArray mapArray = new JSONArray();
        for (Map map : list) {
            JSONObject mapObject = new JSONObject();
            mapObject.put("name",map.get("sname"));
            mapObject.put("value",map.get("count"));
            mapArray.add(mapObject);
        }
        dataObject.put("mapData",mapArray);

        jsonObject.put("data",dataObject);
        return jsonObject;
    };
}
